package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.coupon.YjfkPhotoUploadActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/1.
 */

public class YjfkActivity extends BaseActivity{

    private Context context = this;
    private List<TieBean> stringPhone  = new ArrayList<>();

    @BindView(R.id.lxTxt)
    TextView lxTxt;
    private String lxId;
    private String picImg;
    @BindView(R.id.yjfkContent)
    EditText yjfkContent;

    @BindView(R.id.addphoto)
    ImageView addphoto;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_wyly;
    }

    @Override
    protected void initView() {
        findViewById(R.id.text_top_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(lxId)){
                    ToastUtils.toastShort("请选择意见反馈类型！");
                    return;
                }
                if (TextUtils.isEmpty(yjfkContent.getText().toString())){
                    ToastUtils.toastShort("反馈内容不能为空!");
                    return;
                }
                RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), new File(picImg));
                Call<ResultObjectModel> call = HttpAdapter.getSerives().addFeedBack(fileBody, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), lxId, yjfkContent.getText().toString());
                call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
                    @Override
                    protected void onSuccess(ResultObjectModel resultUploadModel) {

                        if (resultUploadModel.getResultCode().equals("0000")){
                            ToastUtils.toastShort(resultUploadModel.getResultMsg());
                            finish();
                        }else{
                            ToastUtils.toastShort(resultUploadModel.getResultMsg());
                        }

                    }
                });
            }
        });
        findViewById(R.id.lxLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUIUtils.showSheet(context, stringPhone, "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        lxTxt.setText(stringPhone.get(position).getTitle());
                        lxId = stringPhone.get(position).getId()+"";
                    }

                    @Override
                    public void onBottomBtnClick() {
                    }
                }).show();
            }
        });

        findViewById(R.id.addphoto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(context, YjfkPhotoUploadActivity.class),1);
            }
        });
        appfbtc();
    }

    private void appfbtc() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appfbtc(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    List<LinkedTreeMap> linkedTreeMaps = (List<LinkedTreeMap>) linkedTreeMap.get("typeList");
                    stringPhone = new ArrayList<>();
                    for (int i = 0; i < linkedTreeMaps.size(); i++){
                        stringPhone.add(new TieBean(linkedTreeMaps.get(i).get("NAME")+"", Integer.parseInt(decimalFormat.format(linkedTreeMaps.get(i).get("ID")))));
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2){
            Glide.with(this).load(new File(data.getStringExtra("imgFile"))).into(addphoto);
            picImg = data.getStringExtra("imgFile");
        }
    }
}
